/**
 * CaddieFramePane.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

import org.windu2b.jcaddie.model.*;
import org.windu2b.jcaddie.swing.*;
import org.windu2b.jcaddie.tools.*;

/**
 * @author windu.2b
 * 
 */
public class CaddieFramePane extends JRootPane
{
	/**
	 * 
	 */
	private static final long	  serialVersionUID	= -1555069792295568605L;


	private static int	          newCaddieCount;


	private int	                  newCaddieNumber;


	private Caddie	              caddie;


	private CaddieApplication	  application;


	private CaddieFrameController	controller;


	private ResourceBundle	      resource;




	public CaddieFramePane( Caddie caddie, CaddieApplication application,
	        CaddieFrameController controller )
	{
		this.caddie = caddie;
		this.controller = controller;
		this.application = application;
		this.resource = ResourceBundle.getBundle( CaddieFramePane.class
		        .getName() );

		if ( caddie.getName() == null ) newCaddieNumber = ++newCaddieCount;

		setContentPane( controller.getView() );
	}




	public void displayView()
	{
		JFrame caddieFrame = new JFrame()
		{
			/**
			 * 
			 */
			private static final long	serialVersionUID	= 6903062545398324861L;

			{
				setRootPane( CaddieFramePane.this );
			}
		};

		caddieFrame.setIconImage( new ImageIcon( ImageInJAR.getInstance().getImage( URLIcon.APPLICATION.toString() ) )
		        .getImage() );
		updateFrameTitle( caddieFrame, caddie );
		computeFrameBounds( caddieFrame );

		getToolkit().setDynamicLayout( true );
		addListeners( caddie, application, controller, caddieFrame );

		// Réglages propres à Mac OS X
		CaddiePane caddieView = ( CaddiePane ) controller.getView();
		setJMenuBar( caddieView.getJMenuBar() );
		caddieView.setJMenuBar( null );

		// On affiche la fenêtre
		caddieFrame.setVisible( true );
	}




	private void addListeners( final Caddie caddie,
	        final CaddieApplication application,
	        final CaddieFrameController controller, final JFrame frame )
	{
		frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
		frame.addWindowListener( new WindowAdapter()
		{
			@Override
			public void windowClosing( WindowEvent ev )
			{
				controller.close();
			}
		} );

		application.addCaddieListener( new CaddieListener()
		{
			public void caddieChanged( CaddieEvent ev )
			{
				if ( ev.getCaddie() == caddie
				        && ev.getType() == CaddieEvent.Type.DELETE )
				{
					application.removeCaddieListener( this );
					frame.dispose();
				}
			};
		} );

		caddie.addPropertyChangeListener( "name", new PropertyChangeListener()
		{
			public void propertyChange( PropertyChangeEvent evt )
			{
				updateFrameTitle( frame, caddie );
			}
		} );

		caddie.addPropertyChangeListener( "modified",
		        new PropertyChangeListener()
		        {
			        public void propertyChange( PropertyChangeEvent evt )
			        {
				        updateFrameTitle( frame, caddie );
			        }

		        } );

		if ( System.getProperty( "os.name" ).startsWith( "Mac OS X" ) )
		{
			MacOSXConfiguration.bindControllerToApplicationMenu( frame,
			        controller );
		}
	}




	private void computeFrameBounds( JFrame frame )
	{
		//frame.setLocationByPlatform( true );
		frame.pack();

		Dimension screensize = getToolkit().getScreenSize();
		Insets screenInsets = getToolkit().getScreenInsets(
		        getGraphicsConfiguration() );
		screensize.width -= screenInsets.left + screenInsets.right;
		screensize.height -= screenInsets.top + screenInsets.bottom;

		frame.setSize( Math.min( screensize.width * 4 / 5, getWidth() ), Math
		        .min( screensize.height * 4 / 5, getHeight() ) );
	}




	private void updateFrameTitle( JFrame frame, Caddie caddie )
	{
		String name = caddie.getName();
		if ( name == null )
		{
			name = resource.getString( "untitled" );
			if ( newCaddieNumber > 1 ) name += " " + newCaddieNumber;
		}
		else
		{
			name = new File( name ).getName();
		}

		String title = name;

		if ( System.getProperty( "os.name" ).startsWith( "Mac OS X" ) )
		{
			putClientProperty( "windowModified", Boolean.valueOf( caddie
			        .isModified() ) );
		}
		else
		{
			title += " - JCaddie";
			if ( caddie.isModified() ) title = "* " + title;
		}

		frame.setTitle( title );
	}
}